package com.chanyachting.emall.action.member;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Member;
import com.chanyachting.emall.form.MemberForm;
import com.chanyachting.emall.service.MemberService;
import com.chanyachting.emall.serviceImpl.MemberServiceImpl;
import com.chanyachting.emall.util.MyFile;
import com.chanyachting.emall.util.RequestUtil;

public class UpdateInfoAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		
		RequestUtil ru=new RequestUtil(request);
		
		//从session 中获得会员对象
		Member member=(Member)ru.getSessionObject(MemberService.MEMBER_IN_SESSION,new Member());
		
		int id=member.getId();
		
		MemberForm memForm=(MemberForm)form;
		
		BeanUtils.copyProperties(member, memForm);
		
		
		String imgUrl=MyFile.saveFileForm(memForm.getFile(), request);
		//设置会员信息
		
		member.setImgUrl(imgUrl);
		
		String selectYear=ru.getParameter("selectYear", "1930");
		String selectMonth=ru.getParameter("selectMonth", "1");
		String selectDay=ru.getParameter("selectDay", "1");
		
		member.setMemberBirthday(selectYear+"-"+selectMonth+"-"+selectDay);
		
		member.setId(id);
		
		MemberService memberService=new MemberServiceImpl();
		
		//执行数据库更新
		boolean temp=memberService.updateMemberInfo(member);
		
		
		if(temp){
			
			//更新session 中会员信息
			ru.setMsg("会员信息更新成功!");
			
		}else{
			
			ru.setMsg("会员信息更新失败！请稍候再试。");
		}
		
		return mapping.findForward("memberInfo");
	}
	
	
	

}
